﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandBuildRoach : BoCommand
    {
        private const float _min = 75.0f;
        private const float _gas = 25.0f;
        private const float _supply = 2.0f;
        private const int _time = 27;
        private const commandID _commandID = commandID.buildRoach;

        private int _hatchIndex = -1;
        public BoCommandBuildRoach(int hatchIndex)
        {
            _hatchIndex = hatchIndex;
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.removeMineralsAndGas(_min, _gas);
            node.gameState.getHatchery(_hatchIndex).removeLarvaeForUnit();
            node.gameState.startBuildingUnit((int)Units.Roach);

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.finishedBuildingUnit((int)Units.Roach);
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            int nbRequired = goal.nbUnitsRequired((int)Units.Roach);
            int nbCurrently = node.gameState.nbUnitsBeingBuilt((int)Units.Roach) + node.gameState.nbUnitsBuilt((int)Units.Roach);

            if (nbCurrently < nbRequired)
            {
                return true;
            }

            node.dismissCommand((int)commandID.buildRoach);
            return false;
        }
        public static bool isAvailable(BoGameState gs, int hatchIndex)
        {
            if (gs.getHatchery(hatchIndex).nbLarvae < 1)
            {
                return false;
            }
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            if (!gs.isResourcesAvailable(_min, _gas, _supply))
            {
                return false;
            }
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100) + _hatchIndex;
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Hatchery " + _hatchIndex + " -> Roach";
            return str;
        }
    }
}
